package com.itheima.health.task;

import com.itheima.health.common.RedisConst;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SendCodeService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Set;

@Component
@Slf4j
public class TimeTask implements Serializable{

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private QiniuUtils qiniuUtils;
    @DubboReference
    private SetMealService setMealService;
    @DubboReference
    private SendCodeService sendCodeService;
    @DubboReference
    private OrderService orderService;

    /**
     * @Author: lenovo
     * @Date: 16:00 2021/5/8
     * @Parms []
     * @ReturnType: void
     * @Description: 定时任务删除图片七牛云和Redis缓存中
    */
    @Scheduled(cron = "0/5 * * * * ?") //每隔5秒运行一次
    public void task01(){
        System.out.println("定时任务开始执行====" + LocalDateTime.now());
        //1.获取redis 中图片set集合
        Set<String> fileNames = redisTemplate.boundSetOps(RedisConst.PIC_REDIS_PRE).members();
        //2.遍历---》fileName
        for (String fileName : fileNames) {
            //3.截取fileName中的时间戳
            String[] split = fileName.split("_");
            String initTime = split[0];
            //4.计算当前时间与fileName中的时间戳差值
            long l = System.currentTimeMillis() - Long.valueOf(initTime);
            //5.判断时间是否超过30秒
            if(l<30*1000){
             //5.1 未超过30秒---》忽略
                log.info("[定时任务-图片未超时]fileName： {}", fileName);
                continue;
            }
            //5.2 超过30秒--》rpc调用查询是否有使用该图片
            int count = setMealService.conut(fileName);
            if(count <= 0){ //未使用
                //6.如果未使用该图片---》七牛云删除
                log.info("[定时任务-从七牛云删除图片]fileName： {}", fileName);
                qiniuUtils.deleteFileFromQiniu(fileName);
            }
            //7.从redis删除
            log.info("[定时任务-从redis删除图片]fileName： {}", fileName);
            redisTemplate.boundSetOps(RedisConst.PIC_REDIS_PRE).remove(fileName);
        }
    }

    @Scheduled(cron = "0/59 0 0 1/1 * ? ") //每隔一天执行一次
    public void task2(){
        System.out.println("定时任务开始执行====" + LocalDateTime.now());
        //定时任务执行
        Set<Member> members = orderService.findTomMember();
        for (Member member : members) {
            log.info("data[member]:{}", member);
            String phoneNumber = member.getPhoneNumber();
            String name = member.getName();
            String content="亲爱的会员,"+name+",明天体检别迟到喽";
            sendCodeService.sendOrder(phoneNumber,content);
        }
    }
}
